package com.yfp.operation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.*;
import com.yfp.operation.enums.MarketingEnums;
import com.yfp.operation.mapper.*;
import com.yfp.operation.mapper.*;
import com.yfp.operation.service.MarketingService;
import com.yfp.operation.domain.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author admin
 * @description 针对表【marketing(营销主表)】的数据库操作Service实现
 * @createDate 2024-07-17 17:08:44
 */
@Service
@Slf4j
public class MarketingServiceImpl extends ServiceImpl<MarketingMapper, Marketing> implements MarketingService {

    @Resource
    private MarketingMapper marketingMapper;

    @Resource
    private MarActivityMapper marActivityMapper;

    @Resource
    private MarCouponMapper marCouponMapper;

    @Resource
    private MarCouponPackMapper marCouponPackMapper;
    @Resource
    private LotteryRecordsMapper lotteryRecordsMapper;
    @Resource
    private ActivitySignInsMapper activitySignInsMapper;

    @Override
    public String getNameById(Long marketingId) {
        Marketing marketing = getById(marketingId);
        if (marketing == null) {
            return "";
        }
        if (MarketingEnums.COUPON.getType().equals(marketing.getType())) {
            Optional<MarCoupon> marCouponOptional = Optional.ofNullable(marCouponMapper.selectById(marketing.getMarCouponId()));
            return marCouponOptional.map(MarCoupon::getCouponName).orElse("");
        } else if (MarketingEnums.ACTIVITY.getType().equals(marketing.getType())) {
            Optional<MarActivity> marActivityOptional = Optional.ofNullable(marActivityMapper.selectById(marketing.getMarActivityId()));
            return marActivityOptional.map(MarActivity::getActivityName).orElse("");
        } else if (MarketingEnums.PACK.getType().equals(marketing.getType())) {
            Optional<MarCouponPack> marPackOptional = Optional.ofNullable(marCouponPackMapper.getById(marketing.getMarCouponPackId()));
            return marPackOptional.map(MarCouponPack::getPackName).orElse("");
        }else if (MarketingEnums.LOTTERY.getType().equals(marketing.getType())) {
            Optional<LotteryRecords> marPackOptional = Optional.ofNullable(lotteryRecordsMapper.selectById(marketing.getLotteryRecordsId()));
            return marPackOptional.map(LotteryRecords::getName).orElse("");
        }else if (MarketingEnums.ASI.getType().equals(marketing.getType())) {
            Optional<ActivitySignIns> marPackOptional = Optional.ofNullable(activitySignInsMapper.selectById(marketing.getActivitySignInsId()));
            return marPackOptional.map(ActivitySignIns::getName).orElse("");
        }
        return "";
    }

    @Override
    public Long getIdById(Long marketingId) {
        Marketing marketing = getById(marketingId);
        if (marketing == null) {
            return 0L;
        }

        log.info("修改弹窗marketingId:{}", marketingId);

        if (MarketingEnums.COUPON.getType().equals(marketing.getType())) {
            return marketing.getMarCouponId();

        } else if (MarketingEnums.ACTIVITY.getType().equals(marketing.getType())) {
            return marketing.getMarActivityId();
        } else if (MarketingEnums.LOTTERY.getType().equals(marketing.getType())) {
            return marketing.getLotteryRecordsId();
        } else if (MarketingEnums.ASI.getType().equals(marketing.getType())) {
            return marketing.getActivitySignInsId();
        }
        return 0L;
    }

    @Override
    public <T> T getMarById(Long marketingId) {
        Marketing marketing = getById(marketingId);
        if (marketing == null) {
            return null;
        }
        if (MarketingEnums.COUPON.getType().equals(marketing.getType())) {
            return (T) marCouponMapper.selectById(marketing.getMarCouponId());
        } else if (MarketingEnums.ACTIVITY.getType().equals(marketing.getType())) {
            return (T) marActivityMapper.selectById(marketing.getMarActivityId());
        } else if (MarketingEnums.PACK.getType().equals(marketing.getType())) {
            return (T) marCouponPackMapper.getById(marketing.getMarCouponPackId());
        }else if (MarketingEnums.LOTTERY.getType().equals(marketing.getType())) {
            return (T) lotteryRecordsMapper.selectById(marketing.getLotteryRecordsId());
        }else if (MarketingEnums.ASI.getType().equals(marketing.getType())) {
            return (T) activitySignInsMapper.selectById(marketing.getActivitySignInsId());
        }
        return null;
    }
}




